package cn.edu.hncj.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


/**
 * @author Mr.ma
 * @since 2022/11/13 21:18
 **/
@Component
@Aspect
public class TransactionAdvice {
    @Pointcut("execution(* cn.edu.hncj..*.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void checkout(){
        System.out.println("前置通知=====开启事务管理");
    }

    @AfterReturning("pointcut()")
    public void log(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime + "    INFO    后置返回通知=====@AfterReturning");
    }

    @After("pointcut()")
    public void after(){
        System.out.println("后置通知=====提交事务");
    }


    @AfterThrowing("pointcut()")
    public void afterThrowing(){
        System.out.println("异常通知=====事务回滚");
    }

    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint){
        System.out.println("环绕通知=====前置");
        try {
            joinPoint.proceed();
            System.out.println("环绕通知=====后置");
        } catch (Throwable throwable) {
            System.out.println("环绕通知=====异常");
        }finally {
            System.out.println("环绕通知=====最终");
        }
    }
}
